<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="项目名称" prop="projectId">
        <el-select v-model="queryParams.projectId" placeholder="请选择项目名称" clearable style="width: 220px">
          <el-option v-for="dict in dict.type.scene_inspection" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="标段名称" prop="bdId">
        <el-select v-model="queryParams.bdId" placeholder="请选择标段名称" clearable style="width: 220px">
          <el-option v-for="item in bdSelect" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="里程桩号" prop="lczh">
        <el-input v-model="queryParams.lczh" placeholder="请输入里程桩号" clearable style="width: 220px"
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="问题标题" prop="wtbt">
        <el-input v-model="queryParams.wtbt" placeholder="请输入问题标题" clearable style="width: 220px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="存在问题" prop="czwt">
        <el-input v-model="queryParams.czwt" placeholder="请输入存在问题" clearable style="width: 220px"
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="巡查时间">
        <el-date-picker v-model="daterangeXcsj" style="width: 220px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <!--   <el-form-item label="整改时限">
        <el-date-picker v-model="daterangeZgsx" style="width: 220px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>-->
      <el-form-item  prop="queryParams.approvalStatus">
        <el-checkbox v-model="queryParams.allProject" v-hasPermi="['scene:inspectionIssue:allProject']">全部项目</el-checkbox>
<!--        <el-radio v-model="queryParams.approvalStatus" label="全部项目">全部项目</el-radio>-->
<!--        <el-select v-model="queryParams.approvalStatus" placeholder="请选择审批状态" clearable style="width: 220px">-->
<!--          <el-option v-for="dict in dict.type.scene_inspection" :key="dict.value" :label="dict.label"-->
<!--            :value="dict.value" />-->
<!--        </el-select>-->
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

      <el-row :gutter="10" class="mb8 top-right-btn small-line-height">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['scene:inspectionIssue:add']">新增</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
            v-hasPermi="['scene:inspectionIssue:export']">导出</el-button>
        </el-col> -->
      </el-row>
    </el-form>
    <el-table border stripe v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目名称" header-align="center" align="left" prop="projectName" min-width="150px"
        show-overflow-tooltip />
      <el-table-column label="标段名称" header-align="center" align="left" prop="bdName" min-width="150px"
        show-overflow-tooltip />
      <el-table-column label="里程桩号" header-align="center" align="left" prop="lczh" min-width="150px"
        show-overflow-tooltip />
      <el-table-column label="问题标题" align="center" prop="wtbt" min-width="150px" show-overflow-tooltip />
      <!-- <el-table-column label="存在问题" align="center" prop="czwt" min-width="150px" show-overflow-tooltip /> -->
      <el-table-column label="巡查时间" align="center" prop="xcsj" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.xcsj, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="巡查上报人" align="center" prop="xcsbrName" width="100px" show-overflow-tooltip />
      <!-- <el-table-column label="整改要求" align="center" prop="zgyq" min-width="150px" show-overflow-tooltip /> -->
      <el-table-column label="整改时限" align="center" prop="zgsx" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.zgsx, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审批状态" align="center" prop="approvalStatus" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.scene_inspection" :value="scope.row.approvalStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="onView(scope.row)">详情</el-button>
          <el-button v-if="scope.row.approvalStatus == '00' && scope.row.xcsbr == userId" size="mini" type="text"
            icon="el-icon-check" @click="onSubmit(scope.row)" v-hasPermi="['scene:inspectionIssue:edit']">提交</el-button>
          <el-button v-if="scope.row.approvalStatus == '00' && scope.row.xcsbr == userId" size="mini" type="text"
            icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['scene:inspectionIssue:edit']">修改</el-button>
          <el-button v-if="scope.row.approvalStatus == '11' && canAbarbeitung(scope.row)" size="mini" type="text"
            icon="el-icon-edit" @click="onAbarbeitung(scope.row, 1)">整改</el-button>
          <el-button v-if="scope.row.approvalStatus == '22' && scope.row.xcsbr == userId" size="mini" type="text"
            icon="el-icon-edit" @click="onAbarbeitung(scope.row, 2)">复查</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <inspection-issue-add-or-update ref="addOrUpdateRef" :bdSelect="bdSelect" @callBack="handleQuery" />
    <inspection-issue-view ref="viewRef" />
    <inspection-issue-recording ref="recordingRef" @callBack="handleQuery" />
  </div>
</template>

<script>
import { listInspectionIssue, submitInspectionIssue, delInspectionIssue } from "@/api/scene/inspectionIssue";
import { queryBdSelect } from "@/api/base/bsBd";
import { mapGetters } from "vuex";
import inspectionIssueAddOrUpdate from './inspectionIssue-add-or-update.vue';
import InspectionIssueView from './inspectionIssue-view.vue';
import InspectionIssueRecording from './inspectionIssue-recording.vue';
export default {
  components: { inspectionIssueAddOrUpdate, InspectionIssueView, InspectionIssueRecording },
  name: "InspectionIssue",
  dicts: ['scene_inspection'],
  data () {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      bdSelect: [],
      // 现场巡查问题表格数据
      dataList: [],
      // 审批状态时间范围
      daterangeXcsj: [],
      // 审批状态时间范围
      daterangeZgsx: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: undefined,
        bdId: undefined,
        lczh: undefined,
        wtbt: undefined,
        czwt: undefined,
        xcsj: undefined,
        zgyq: undefined,
        zgsx: undefined,
        fileId: undefined,
        approvalStatus: undefined,
        allProject:undefined
      },
    };
  },
  // 监听顶部全局下拉
  computed: {
    ...mapGetters(["sysProjectId", "sysProjectList", "userId"])
  },
  watch: {
    sysProjectId (newVal, oldVal) {
      this.queryParams.projectId = this.sysProjectId;
      // 系统顶部选中项目变化
      this.init();
    }
  },
  created () {
    this.queryParams.projectId = this.sysProjectId;
    this.init();
  },
  methods: {
    init () {
      this.getBdList();
      this.getList();
    },
    // 获取标段下拉
    getBdList () {
      queryBdSelect({ projectId: this.sysProjectId }).then(response => {
        this.bdSelect = response.data;
      });
    },

    /** 查询现场巡查问题列表 */
    getList () {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeXcsj && '' != this.daterangeXcsj) {
        this.queryParams.params["beginXcsj"] = this.daterangeXcsj[0];
        this.queryParams.params["endXcsj"] = this.daterangeXcsj[1];
      }
      if (null != this.daterangeZgsx && '' != this.daterangeZgsx) {
        this.queryParams.params["beginZgsx"] = this.daterangeZgsx[0];
        this.queryParams.params["endZgsx"] = this.daterangeZgsx[1];
      }
      listInspectionIssue(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.daterangeXcsj = [];
      this.daterangeZgsx = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.$refs.addOrUpdateRef.init();
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.$refs.addOrUpdateRef.init(row.id);
    },

    // 详情
    onView (row) {
      this.$refs.viewRef.init(row.id);
    },
    // 提交
    onSubmit (row) {
      this.$modal.confirm(`是否确认提交现场巡查问题标题为'${row.wtbt}'的数据项？`).then(() => {
        this.loading = true;
        const params = {
          id: row.id
        };
        return submitInspectionIssue(params);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("提交成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal.confirm(`是否确认删除现场巡查问题标题为'${row.wtbt}'的数据项？`).then(() => {
        this.loading = true;
        return delInspectionIssue(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    // 是否可整改
    canAbarbeitung (row) {
      const { assignPeopleId } = row;
      if (!assignPeopleId) {
        return false;
      }
      const arr = assignPeopleId.split(',');
      const userId = arr.find(item => item == this.userId);
      return userId ? true : false;
    },
    onAbarbeitung (row, type) {
      //  1: 整改，2: 复查
      this.$refs.recordingRef.init(row.id, type);
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('scene/inspectionIssue/export', {
        ...this.queryParams
      }, `现场巡查_${new Date().getTime()}.xlsx`);
    }
  }
};
</script>
